home *** CD-ROM | disk | FTP | other *** search
/ APDL Other Worlds / APDL Other Worlds Collection.iso / SF3000 / Extras / !SFcolours / c / Utils < prev   
Encoding:
Text File  |  2003-10-24  |  5.7 KB  |  178 lines

  1. /*
  2.  *  SFcolours - Star Fighter 3000 colours editor
  3.  *  Utility functions
  4.  *  Copyright (C) 2001  Chris Bazley
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public Licence as published by
  8.  *  the Free Software Foundation; either version 2 of the Licence, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public Licence for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public Licence
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /* ANSI library files */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <limits.h>
  26.  
  27. /* RISC OS library files */
  28. #include "kernel.h"
  29. #include "toolbox.h"
  30. #include "event.h"
  31. #include "window.h"
  32. #include "wimplib.h"
  33. #include "gadgets.h"
  34. #include "swis.h"
  35. #include "flex.h"
  36.  
  37. /* My library files */
  38. #include "ViewsMenu.h"
  39. #include "err.h"
  40. #include "msgtrans.h"
  41. #include "hourglass.h"
  42. #include "Macros.h"
  43. #include "FilePerc.h"
  44.  
  45. /* Local headers */
  46. #include "Utils.h"
  47. #include "Main.h"
  48.  
  49. #ifdef INT_COLOUR_FIND
  50. /* OS 2 ColourTrans used R2,G3,B1 and OS 3 ColourTrans uses R2,G4,B1 */
  51. #define RED_WEIGHT   2
  52. #define GREEN_WEIGHT 4
  53. #define BLUE_WEIGHT  1
  54. #define SQUARE(n) ((n)*(n))
  55. #endif
  56.  
  57. /* ----------------------------------------------------------------------- */
  58. /*                         Public functions                                */
  59.  
  60. char brightness_of_24bit_col(int colour)
  61. {
  62.   int r,g,b;
  63.   
  64.   r = (colour & 0x0000ff00) >> 8;
  65.   g = (colour & 0x00ff0000) >> 16;
  66.   b = (colour & 0xff000000) >> 24;
  67.   /* CIE luminance weights for red, green and blue (0.299, 0.587, 0.114), which is the standard conversion for broadcast television */
  68.  return (char)((int)(0.299*(float)r) + (int)(0.587*(float)g) + (int)(0.114*(float)b));
  69. }
  70.  
  71. /* ----------------------------------------------------------------------- */
  72.  
  73. void set_24bit_button_col(ObjectId window, ComponentId button, int colour)
  74. {
  75.   /* Set background colour of button gadget to a 24-bit colour */
  76.   char validation[16];
  77.   
  78.   sprintf(validation, "C/%X", (colour & 0xffffff00) >> 8);
  79.   RE(button_set_validation(0, window, button, validation));
  80.   if(brightness_of_24bit_col(colour) > 128) {
  81.     RE(button_set_flags(0, window, button, WimpIcon_FGColour*0x0f, WimpIcon_FGColour*0x07));
  82.   }
  83.   else {
  84.     RE(button_set_flags(0, window, button, WimpIcon_FGColour*0x0f, WimpIcon_FGColour*0x00));
  85.  }
  86. }
  87.  
  88. /* ----------------------------------------------------------------------- */
  89.  
  90. _kernel_oserror *open_topleftofwin(unsigned int flags, ObjectId showobj, ObjectId relativeto, ObjectId parent, ComponentId parent_component)
  91. {
  92.   WimpGetWindowStateBlock winstate;
  93.   WindowShowObjectBlock showblock;
  94.  
  95.   THROW(window_get_wimp_handle(0, relativeto, &(winstate.window_handle)));
  96.   THROW(wimp_get_window_state(&winstate));
  97.   showblock.visible_area.xmin = winstate.visible_area.xmin+64;
  98.   showblock.visible_area.ymin = winstate.visible_area.ymax-64;
  99.   return toolbox_show_object(flags, showobj, Toolbox_ShowObject_TopLeft, &showblock, parent, parent_component);
  100. }
  101.  
  102. /* ----------------------------------------------------------------------- */
  103.  
  104. _kernel_oserror *show_win_at_ptr(unsigned int flags, ObjectId id, ObjectId parent, ComponentId parent_component)
  105. {
  106.   unsigned int state;
  107.   
  108.   THROW(toolbox_get_object_state(0, id, &state))
  109.   if(FLAG_SET(state, Toolbox_GetObjectState_Showing))
  110.     /* Already open */
  111.     THROW(ViewsMenu_show_object(flags, id, Toolbox_ShowObject_Default, NULL, parent, parent_component))
  112.   else
  113.     /* Not open (can't very well be iconised!) */
  114.     THROW(toolbox_show_object(flags, id, Toolbox_ShowObject_AtPointer, NULL, parent, parent_component))
  115.  
  116.   return NULL; /* success */
  117. }
  118.  
  119. /* ----------------------------------------------------------------------- */
  120.  
  121. char *tail(char *pathname, int length)
  122. {
  123.   char *ptr;
  124.   int dotcount;
  125.   
  126.   ptr = (char *)((int)pathname + strlen(pathname)); /* terminator */
  127.   dotcount = 0;
  128.   while(ptr > pathname && dotcount<length) {
  129.     ptr--; /* scan string backwards from terminator */
  130.     if(*ptr == '.')
  131.       dotcount++;
  132.   }
  133.   if(dotcount >= length)
  134.     return (char *)((int)ptr + 1);
  135.   return ptr;
  136. }
  137.  
  138. /* ----------------------------------------------------------------------- */
  139.  
  140. char real_to_mode13col(unsigned int real_col)
  141. {
  142.   /* Find nearest to ideal colour in default mode 13 palette */
  143. #ifdef INT_COLOUR_FIND
  144.   if(use_colour_trans) {
  145. #endif
  146.     int nearest_colour;
  147.     RE(_swix(ColourTrans_ReturnColourNumberForMode, _INR(0,2)|_OUT(0), real_col, 13, 0, &nearest_colour))
  148.     return nearest_colour;
  149. #ifdef INT_COLOUR_FIND
  150.   } else {
  151.     /* Like ColourTrans we use a least squares function 
  152.        but we have control over the weights */
  153.     char best_col;
  154.     int least_dist = INT_MAX;
  155.     int target_red = (real_col & 0xff00)>>8;
  156.     int target_green = (real_col & 0xff0000)>>16;
  157.     int target_blue = (real_col & 0xff000000)>>24;
  158.     for(int colnum = 0; colnum <= 255; colnum++) {
  159.       int dist = RED_WEIGHT*SQUARE(((palette[colnum] & 0xff00)>>8) - target_red)
  160.       + GREEN_WEIGHT*SQUARE(((palette[colnum] & 0xff0000)>>16) - target_green)
  161.       + BLUE_WEIGHT*SQUARE(((palette[colnum] & 0xff000000)>>24) - target_blue);
  162.       if(dist < least_dist) {
  163.         least_dist = dist;
  164.         best_col = colnum;
  165.       }
  166.     }
  167.     return best_col;
  168.   }
  169. #endif
  170. }
  171.  
  172. /* ----------------------------------------------------------------------- */
  173.  
  174. _kernel_oserror *load_compressed(char *filepath, flex_ptr buffer_anchor)
  175. {
  176.   return perc_operation(FILEPERC_OP_DECOMP, filepath, 0, buffer_anchor);
  177. }
  178.